﻿#include "commentmanage.h"
#include "ui_commentmanage.h"

CommentManage::CommentManage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommentManage)
{
    ui->setupUi(this);

    m_isAll = true;
    m_starSelect = S_All;
    m_replyComment = nullptr;
    m_commentListTemp = new CommentInfoList();
    ui->tabWidget->setCurrentIndex(0);

    QDate date = QDate::currentDate();
    ui->de_comStart->setDate(date);
    ui->de_comEnd->setDate(date);
    ui->de_comStart->setDisplayFormat("yyyy.M.d");
    ui->de_comEnd->setDisplayFormat("yyyy.M.d");
    ui->de_comStart->setCalendarPopup(true);
    ui->de_comEnd->setCalendarPopup(true);

}

CommentManage::~CommentManage()
{
    delete ui;
}

void CommentManage::slotSetCommentList(bool result,const QString& msg)
{
    if (result)
    {
        m_commentListTemp->append(GlobalVars::g_localComment);
        addListShow(m_commentListTemp->end()-1);
    }
    else
    {
        qDebug() << "CommentManage::slotSetCommentList " << msg;
    }
}
void CommentManage::slotSetCommentImage(bool result, const QString& msg, const QImage&image)
{
    if (result)
    {
        for (int i = 0; i < m_commentVector.size(); i++)
        {
            if (msg == m_commentVector[i]->getCommentID())
            {
                m_commentVector[i]->setCommentImage(image);
                break;
            }
        }
    }
    else
    {
        qDebug() << "CommentManage::slotSetCommentImage" << msg;
    }
}
void CommentManage::slotRelayComment(const QString& commentID)
{
    if (m_replyComment != nullptr)
    {
        delete m_replyComment;
    }
    m_replyComment = new ReplyComment();
    m_replyComment->setCommentID(commentID);
    m_replyComment->setWindowTitle("商家点评评论");
    m_replyComment->setWindowModality(Qt::ApplicationModal);
    m_replyComment->setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint); // 禁止最大化按钮
    m_replyComment->setFixedSize(m_replyComment->width(),m_replyComment->height());   // 禁止拖动窗口大小
    m_replyComment->show();

    connect(m_replyComment,SIGNAL(signalSubmitBusComment(const QString &,const QString&)),
            this,SIGNAL(signalSubmitBusComment(const QString &,const QString&)));
}

void CommentManage::setOptionalHotel()
{
    ui->cb_selectHotel->clear();
    m_hotelIdNameMap.clear();
    for(HotelInfoList::iterator it = GlobalVars::g_hotelManageList->begin();
        it != GlobalVars::g_hotelManageList->end(); it++)
    {
        m_hotelIdNameMap.insert(it->getHotel(),it->getName());
    }
    qDebug() << m_hotelIdNameMap.values();
    ui->cb_selectHotel->addItems(m_hotelIdNameMap.values());
}

QString CommentManage::getCurrentSelectedHotelID()
{
    return m_hotelIdNameMap.key(ui->cb_selectHotel->currentText());
}

void CommentManage::on_cb_selectHotel_currentIndexChanged(const QString &arg1)
{
    QString hotelID = m_hotelIdNameMap.key(arg1);
    if (GlobalVars::g_hotelManageMap.contains(hotelID))
    {
        HotelInfoList::iterator it = GlobalVars::g_hotelManageMap[hotelID];
        ui->lb_hotelName->setText(it->getName());
        ui->lb_hotelMark->setText(it->getMark());
        ui->lb_commentCount->setText(QString::number(it->getCommentCount()));

        double star = it->getMark().toDouble();
        if (star == 5.0)
        {
            ui->lb_hotelStar->setText(" ★★★★★");
        }
        else if (star >= 4.0)
        {
            ui->lb_hotelStar->setText(" ★★★★☆");
        }
        else if (star >= 3.0)
        {
            ui->lb_hotelStar->setText(" ★★★☆☆");
        }
        else if (star >= 2.0)
        {
            ui->lb_hotelStar->setText(" ★★☆☆☆");
        }
        else
        {
            ui->lb_hotelStar->setText(" ★☆☆☆☆");
        }
    }

    if (!hotelID.isEmpty())
    {
        emit signalRefreshComment("0",hotelID);
    }
}

void CommentManage::slotSubmitBusCommentResult(bool result,const QString& msg)
{
    if (result)
    {
        m_replyComment->close();
        slotCommentListClean();
        emit signalRefreshComment("0",m_hotelIdNameMap.key(ui->cb_selectHotel->currentText()));
    }
    else
    {
        m_replyComment->errorShow(msg);
    }
}

void CommentManage::slotCommentListClean()
{
    m_commentVector.clear();

    while(ui->lwg_commentAll->count() > 0)
    {
        QListWidgetItem * item = ui->lwg_commentAll->takeItem(0);
        delete item;
    }
    while(ui->lwg_commentNo->count() > 0)
    {
        QListWidgetItem * item = ui->lwg_commentNo->takeItem(0);
        delete item;
    }
}

void CommentManage::on_tabWidget_currentChanged(int index)
{
    if (index == 0)
    {
        m_isAll = true;
    }
    else
    {
        m_isAll = false;
    }

    refrushShow();
}

void CommentManage::judgeDaySelectState()
{
    switch (m_daySelect)
    {
    case D_None: break;
    case D_WeekAll:
    case D_WeekNo:
        ui->pb_comWeek->setStyleSheet("background-color: #13d1be;");
        ui->pb_comMonth->setStyleSheet("background-color: transparent;");
        ui->de_comStart->setStyleSheet("background-color: none;");
        ui->de_comEnd->setStyleSheet("background-color: none;");
        break;
    case D_MonthAll:
    case D_MonthNo:
        ui->pb_comWeek->setStyleSheet("background-color: transparent;");
        ui->pb_comMonth->setStyleSheet("background-color: #13d1be;");
        ui->de_comStart->setStyleSheet("background-color: none;");
        ui->de_comEnd->setStyleSheet("background-color: none;");
        break;
    case D_CustomizeAll:
    case D_CustomizeNo:
        ui->pb_comWeek->setStyleSheet("background-color: transparent;");
        ui->pb_comMonth->setStyleSheet("background-color: transparent;");
        ui->de_comStart->setStyleSheet("background-color: #13d1be;");
        ui->de_comEnd->setStyleSheet("background-color: #13d1be;");
        break;
    default: break;
    }
    refrushShow();
}
void CommentManage::judgeStarSelectState()
{
    switch (m_starSelect)
    {
    case S_All:
        ui->pb_starAll->setStyleSheet("background-color: #13d1be;");
        ui->pb_starMid->setStyleSheet("background-color: transparent;");
        ui->pb_starGood->setStyleSheet("background-color: transparent;");
        break;
    case S_One2Three:
        ui->pb_starAll->setStyleSheet("background-color: transparent;");
        ui->pb_starMid->setStyleSheet("background-color: #13d1be;");
        ui->pb_starGood->setStyleSheet("background-color: transparent;");
        break;
    case S_Four2Five:
        ui->pb_starAll->setStyleSheet("background-color: transparent;");
        ui->pb_starMid->setStyleSheet("background-color: transparent;");
        ui->pb_starGood->setStyleSheet("background-color: #13d1be;");
        break;
    }
    refrushShow();
}

void CommentManage::on_pb_starAll_clicked()
{
    m_starSelect = S_All;
    judgeStarSelectState();
}
void CommentManage::on_pb_starMid_clicked()
{
    m_starSelect = S_One2Three;
    judgeStarSelectState();
}
void CommentManage::on_pb_starGood_clicked()
{
    m_starSelect = S_Four2Five;
    judgeStarSelectState();
}
void CommentManage::on_pb_comWeek_clicked()
{
    if (m_isAll)
    {
        m_daySelect = D_WeekAll;
    }
    else
    {
        m_daySelect = D_WeekNo;
    }
    judgeDaySelectState();
}
void CommentManage::on_pb_comMonth_clicked()
{
    if (m_isAll)
    {
        m_daySelect = D_MonthAll;
    }
    else
    {
        m_daySelect = D_MonthNo;
    }
    judgeDaySelectState();
}
void CommentManage::on_de_comStart_dateChanged(const QDate &date)
{
    m_startDay = date;
    if (m_isAll)
    {
        m_daySelect = D_CustomizeAll;
    }
    else
    {
        m_daySelect = D_CustomizeNo;
    }
    judgeDaySelectState();
}
void CommentManage::on_de_comEnd_dateChanged(const QDate &date)
{
    m_endDay = date;
    if (m_isAll)
    {
        m_daySelect = D_CustomizeAll;
    }
    else
    {
        m_daySelect = D_CustomizeNo;
    }
    judgeDaySelectState();
}

void CommentManage::refrushShow()
{
    m_commentListTemp->clear();
    slotCommentListClean();

    switch (m_daySelect)
    {
    case D_None: break;
    case D_WeekAll:
        switch (m_starSelect)
        {
            case S_All:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    QDate date = QDate::fromString(it->getCommentTime(),"yyyy-MM-dd");
                    if (date.addDays(7) >= QDate::currentDate())
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
            case S_One2Three:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    double star = it->getStar().toDouble();
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (star > 0 && star < 4.0 && date.addDays(7) >= QDate::currentDate())
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
            case S_Four2Five:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    double star = it->getStar().toDouble();
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (star >=4.0 && star <= 5.0 && date.addDays(7) >= QDate::currentDate())
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
        }  break;
    case D_MonthAll:
        switch (m_starSelect)
        {
            case S_All:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (date.addMonths(1) >= QDate::currentDate())
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
            case S_One2Three:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    double star = it->getStar().toDouble();
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (star > 0 && star < 4.0 && date.addMonths(1) >= QDate::currentDate())
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
            case S_Four2Five:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    double star = it->getStar().toDouble();
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (star >=4.0 && star <= 5.0 && date.addMonths(1) >= QDate::currentDate())
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
        }  break;
    case D_CustomizeAll:
        switch (m_starSelect)
        {
            case S_All:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (date >= m_startDay && date <= m_endDay)
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
            case S_One2Three:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    double star = it->getStar().toDouble();
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (star > 0 && star < 4.0 && date >= m_startDay && date <= m_endDay)
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
            case S_Four2Five:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    double star = it->getStar().toDouble();
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (star >=4.0 && star <= 5.0 && date >= m_startDay && date <= m_endDay)
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
        }  break;

    case D_WeekNo:
        switch (m_starSelect)
        {
            case S_All:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (date.addDays(7) >= QDate::currentDate() && it->getHotelComment().isEmpty())
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
            case S_One2Three:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    double star = it->getStar().toDouble();
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (star > 0 && star < 4.0 && date.addDays(7) >= QDate::currentDate()  && it->getHotelComment().isEmpty())
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
            case S_Four2Five:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    double star = it->getStar().toDouble();
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (star >=4.0 && star <= 5.0 && date.addDays(7) >= QDate::currentDate()  && it->getHotelComment().isEmpty())
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
        }  break;
    case D_MonthNo:
        switch (m_starSelect)
        {
            case S_All:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (date.addMonths(1) >= QDate::currentDate()  && it->getHotelComment().isEmpty())
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
            case S_One2Three:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    double star = it->getStar().toDouble();
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (star > 0 && star < 4.0 && date.addMonths(1) >= QDate::currentDate()  && it->getHotelComment().isEmpty())
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
            case S_Four2Five:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    double star = it->getStar().toDouble();
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (star >=4.0 && star <= 5.0 && date.addMonths(1) >= QDate::currentDate()  && it->getHotelComment().isEmpty())
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
        }  break;
    case D_CustomizeNo:
        switch (m_starSelect)
        {
            case S_All:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (date >= m_startDay && date <= m_endDay  && it->getHotelComment().isEmpty())
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
            case S_One2Three:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    double star = it->getStar().toDouble();
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (star > 0 && star < 4.0 && date >= m_startDay && date <= m_endDay  && it->getHotelComment().isEmpty())
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
            case S_Four2Five:
                for(CommentInfoList::iterator it = GlobalVars::g_commentInfoList->begin();it != GlobalVars::g_commentInfoList->end(); it++)
                {
                    double star = it->getStar().toDouble();
                    QDate date = QDate::fromString(it->getCommentTime().left(10),"yyyy-MM-dd");
                    if (star >=4.0 && star <= 5.0 && date >= m_startDay && date <= m_endDay  && it->getHotelComment().isEmpty())
                    {
                        m_commentListTemp->append(*it);
                    }
                } break;
        }  break;
    default: break;
    }

    for(CommentInfoList::iterator it = m_commentListTemp->begin();it != m_commentListTemp->end(); it++)
    {
        addListShow(it);
    }
}
void CommentManage::addListShow(const CommentInfoList::iterator& it)
{
    int index = m_commentVector.size();
    HotelDetailsCommentContent *commentContent = new HotelDetailsCommentContent();
    commentContent->setCommentInfo(*it);
    if (GlobalVars::g_commentImageListMap.contains(it->getComment()))
    {
        commentContent->setCommentImage(*GlobalVars::g_commentImageListMap[it->getComment()]);
    }
    m_commentVector.push_back(commentContent);

    if (m_isAll)
    {
        ui->lwg_commentAll->addItem("");
        ui->lwg_commentAll->setItemWidget(ui->lwg_commentAll->item(index),commentContent);
        ui->lwg_commentAll->item(index)->setSizeHint(QSize(0, 160));  //每次改变Item的高度
    }
    else
    {
        if (it->getHotelComment().isEmpty())
        {
            ui->lwg_commentNo->addItem("");
            ui->lwg_commentNo->setItemWidget(ui->lwg_commentNo->item(index),commentContent);
            ui->lwg_commentNo->item(index)->setSizeHint(QSize(0, 160));  //每次改变Item的高度

            commentContent->setRelayEnabled();
            connect(commentContent,SIGNAL(signalRelayComment(const QString&)),
                    this,SLOT(slotRelayComment(const QString&)));
        }
    }
}
